﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CQIE.OVS.BLL;
using CQIE.OVS.Model;
using CQIE.OVS.Web.Browser.Models;

namespace CQIE.OVS.Web.Browser.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Active(int AId = 3)
        {
            ViewBag.User = Session["User"];
            //总票数
            var model = getActive(AId);
            Session["AId"] = model;
            return View(model.Campaigners);
        }

        /// <summary>
        /// 注册 0：存在 1：成功 3:成功且登录成功 4：成功但登录失败
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int Register(string account, string pwd, string autoLogin = "No")
        {
            UserBLL bll = new UserBLL();
            IList<QueryConditions> query = new List<QueryConditions>();
            query.Add(new QueryConditions("Account", "=", account));
            var list = bll.Find(query);
            if (list.Count > 0)
            {
                return 0;
            }

            Model.User model = new User()
            {
                Account = account,
                Password = pwd,
                root = 1
            };
            bll.Create(model);
            if (autoLogin == "No")
            {
                return 1;
            }

            if (Login(account, pwd)==1)
            {
                return 3;
            }

            return 4;

        }


        /// <summary>
        /// 登录 返回1：成功 ；0：失败
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int Login(string account, string pwd)
        {
            UserBLL bll = new UserBLL();
            IList<QueryConditions> quers = new List<QueryConditions>();
            quers.Add(new QueryConditions("Account", "=", account));
            quers.Add(new QueryConditions("Password", "=", pwd));
            quers.Add(new QueryConditions("root", "=", 1));
            var list = bll.Find(quers);
            if (list.Count > 0)
            {
                Session["User"] = list[0];
                return 1;
            }

            return 0;
        }

        public void Logout()
        {
            Session["User"] = null;
        }

        private Activity getActive(int AId)
        {
            ActivityBLL bll = new ActivityBLL();
            Activity model = bll.Get(AId);
            model.Campaigners = bll.GetCampaignerList(model);
            return model;
        }

        /// <summary>
        /// 投票操作
        /// </summary>
        /// <param name="CId"></param>
        public string VoteGo(int CId)
        {
            if (Session["User"] == null)
            {
                return "您还未登录，请先登录！";
            }

            if (Session["Aid"] == null)
            {
                return "比赛信息异常！";
            }

            var active = Session["AId"] as Activity;
            if (DateTime.Now > active.DateEnd)
            {
                return "比赛已结束！无法继续投票！";
            }

            if (DateTime.Now < active.DateStart)
            {
                return "比赛未开始！";
            }

            CampaignerBLL bll = new CampaignerBLL();
            Campaigner Cam = bll.Get(CId);
            //保存投票信息
            Vote model = new Vote();
            model.Activity = active;
            model.User = Session["User"] as User;
            model.Campaigner = Cam;
            model.VoteTime = DateTime.Now;

            if (model.User.Account != "admin")
            {
                IList<QueryConditions> querys = new List<QueryConditions>();
                querys.Add(new QueryConditions("User", "=", model.User));
                querys.Add(new QueryConditions("Activity", "=", model.Activity));
                var History = new VoteBLL().Find(querys);
                foreach (Vote item in History)
                {
                    if (item.VoteTime.Year == DateTime.Now.Year
                        && item.VoteTime.Month == DateTime.Now.Month
                        && item.VoteTime.Day == DateTime.Now.Day)
                    {
                        return "一天仅能投一票";
                    }
                }
            }

            new VoteBLL().Create(model);
            //更新票数
            Cam.Num = Cam.Num + 1;
            bll.Update(Cam);
            return "OK";
        }


        public JsonResult CountHot()
        {
            var model =new ActivityBLL().Get((Session["AId"] as Activity).ID);

            int all = GetAllTickets(model);
            var data = model.Campaigners.Select(n => new VoteModel()
            {
                Hot = ((float)n.Num / all).ToString("P"),
                ID = n.ID,
                Num = n.Num
            });
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public int GetAllTickets(Activity model)
        {
            int AllCount = 0;
            foreach (Campaigner item in model.Campaigners)
            {
                AllCount = AllCount + item.Num;
            }

            return AllCount;
        }

    }
}